﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;


public partial class UserControl_ControlRegisterTips : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetTips();
        }
    }
    void GetTips()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getTip", CommandType.StoredProcedure);
        if (db.Rows.Count > 0)
        {
            GridView1.DataSource = db;
            GridView1.DataBind();
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        string filenameFileHtmlVN = "";
        string filenameFileHtmlEN = "";
        string filenameFileImageVN = "";
        if (FileHtmlVN.HasFile)
        {
            try
            {
                filenameFileHtmlVN = Path.GetFileName(FileHtmlVN.FileName);
                FileHtmlVN.SaveAs(Server.MapPath("~/HtmlTips/") + filenameFileHtmlVN);
               
            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage("Upload status1: The file could not be uploaded. The following error occured: " + ex.Message, uscMsgBox.enmMessageType.Error);
            }
        }

        if (FileHtmlEN.HasFile)
        {
            try
            {
                filenameFileHtmlEN = Path.GetFileName(FileHtmlEN.FileName);
                FileHtmlEN.SaveAs(Server.MapPath("~/HtmlTips/") + filenameFileHtmlEN);
               
            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage("Upload status2: The file could not be uploaded. The following error occured: " + ex.Message, uscMsgBox.enmMessageType.Error);
            }
        }
        if (FileImageVN.HasFile)
        {
            try
            {
                filenameFileImageVN = Path.GetFileName(FileImageVN.FileName);
                FileImageVN.SaveAs(Server.MapPath("~/ImageTip/") + filenameFileImageVN);
               
            }
            catch(Exception ex)
            {
                uscMsgBox1.AddMessage("Upload status2: The file could not be uploaded. The following error occured: " + ex.Message, uscMsgBox.enmMessageType.Error);
            }
        }
        try
        {
            dt = new ClassExecute.ClassExecute();
            TxtMaTip.Text = dt.GetDataDepend(str, "k_insertTip", CommandType.StoredProcedure, ref error,
                new SqlParameter("@TitleVN", TxtTitleVN.Text),
                new SqlParameter("@TitleEN", txtTitleEN.Text),
                new SqlParameter("@ImagePath", filenameFileImageVN),
                new SqlParameter("@Active", CheckActive.Checked),
                new SqlParameter("@TipUrlVN",filenameFileHtmlVN),
                new SqlParameter("@TipUrlEN",filenameFileHtmlEN));
            GetTips();
            uscMsgBox1.AddMessage("Khai báo mẫu thông tin sức khỏe thành công", uscMsgBox.enmMessageType.Success);

        }
        catch
        {
            uscMsgBox1.AddMessage("Khai báo mẫu thông tin sức khỏe thất bại", uscMsgBox.enmMessageType.Success);
        }

    }
    protected void ImgFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
    }
    protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GetTips();
    }
    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
    }
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
        dt = new ClassExecute.ClassExecute();
        bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_ActiveDeActiveTip", ref error,
            new SqlParameter("@TipID", keys));
        if (kq)
        {
            uscMsgBox1.AddMessage("Nghiệp vụ thành công", uscMsgBox.enmMessageType.Success);
        }
        else
        {
            uscMsgBox1.AddMessage("Nghiệp vụ thất bại", uscMsgBox.enmMessageType.Success);
        }
    }
    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GetTips();
    }
}